package com.oa.common.utils;

import org.ofdrw.converter.GeneralConvertException;
import org.ofdrw.converter.ImageMaker;
import org.ofdrw.reader.DLOFDReader;
import org.ofdrw.reader.OFDReader;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import static org.ofdrw.converter.ConvertHelper.ofd2pdf;

/**
 * @program: electroniclicense_xinxiang
 * @description: ofd转jpg和pdf
 * @author: jiangwei
 * @create: 2021-11-27 15:53
 **/

public class OfdConver {
    public static void main(String[] args) throws IOException {
//        toPng("D:/downloadFile/ceshi.ofd", "D:/downloadFile","1");
        String s = toPng("C:\\Users\\dell\\Desktop\\123.ofd", "C:\\Users\\dell\\Desktop\\", "1");
        System.out.println(s);
//        Path input = Paths.get("D:/downloadFile/ceshi.ofd");
//        Path output = Paths.get("D:/downloadFile/1.pfd");
//        toPdf(input, output);

    }


  /*  *
     * @Description: ofd转jpg
     * @Param: * @param null:
     * @return: * @return: null
     * @Author: Mr.JZQ
     * @Date: 2021年04月17日 0017

   */

    public static String toPng(String filename, String dirPath, String jpgName) throws IOException {
        Files.createDirectories(Paths.get(dirPath));
        //filename文件路径到文件名
        String url ="";
        Path src = Paths.get(filename);
        ArrayList<String> list = new ArrayList<>();
        ImageMaker imageMaker = new ImageMaker(new OFDReader(src), 15);
        imageMaker.config.setDrawBoundary(false);
        for (int i = 0; i < imageMaker.pageSize(); i++) {
            BufferedImage image = imageMaker.makePage(i);
           //文件要存的路径 第二个是文件名
            Path dist = Paths.get(dirPath, jpgName +i+ ".jpg");
            ImageIO.write(image, "PNG", dist.toFile());
            if (i == imageMaker.pageSize()-1){
                url+=dirPath+jpgName+i+".jpg";
            }else {
                url+=dirPath+jpgName+i+".jpg"+",";
            }

        }
        return url;
    }


    /**
     * 转PDF
     *
     * @param input  OFD输入文件
     * @param output PDF输出文件路径
     * @throws IllegalArgumentException 参数错误
     * @throws GeneralConvertException  文档转换过程中异常
     */
    public static void toPdf(Path input, Path output) {
        ofd2pdf(input, output);
    }
}
